<!--
- Author:   .
- Date:    2017/7/11 0011.
- File:    list.
-->
<template>
  <div>
    <div class="Web_Box">
      <div class="DistributionOrder">
        <div class="ProList">
          <ul
            v-infinite-scroll="loadMoreOrder"
            infinite-scroll-disabled="false"
            infinite-scroll-distance="0"
            infinite-scroll-immediate-check="false">
            <li v-for="order in orderList.data">
              <div class="tit"><span class="fl">订单号：{{order.order_sn}}</span>
              </div>
              <div style="margin-bottom: 10px;" class="giftbox f-cb" v-for="goods in order.order_goods"  @click="gotoOrderDetail(order.order_id)">
                <div class="Pic">
                  <img :src="$config.website + $config.goodsImgSrc + goods.goods_image" alt=""/>
                </div>
                <div class="Con" style="width:11.5rem;">
                  <div class="">
                    <h2 class="">{{goods.goods_name}}  <span v-for="attr in goods.goods_spec">{{attr.name}} </span></h2>
                  </div>
                  <div class="">
                    <span class="Prices" v-show="goods.gc_id != '1038'">￥{{goods.goods_price}}</span>
                    <span class="Prices" v-show="goods.gc_id == '1038'"><i class="iconfont icon-zengpin1"></i></span>
                    <span class="Num fr">×{{goods.goods_num}}</span>
                  </div>
                </div>
              </div>
              <div class="Operation">
                <div class="Deductible">获得积分:{{(!userLv || userLv == $dictionary.userLv.a) ? parseFloat(order.order_amount) + parseFloat(order.freeze_balance) : parseFloat(order.freeze_balance)}}</div>
                <p class="Total tr">共{{countOrderGoodsNum(order)}}件商品&nbsp;&nbsp;合计:￥{{order.order_amount || 0}}</p>
              </div>
            </li>
          </ul>
          <loading-more :allLoaded="orderList.allLoaded" :show="orderList.data.length > 0"></loading-more>
        </div>
      </div>
    </div>
    <empty-data :show="orderList.allLoaded && orderList.data.length <= 0"></empty-data>
    <footer-tab></footer-tab>
  </div>
</template>

<script>
  import {MessageBox} from 'mint-ui'
  import payPop from '../../components/payPop'

  export default{
    data() {
      return {
        userLv: this.$route.query.userLv, // 默认0，全部类型
        memberId: this.$route.query.memberId,
        s_time: this.$route.query.s_time || '', // 指定开始月份
        e_time: this.$route.query.e_time || '', // 指定结束月份
        orderList: {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
      }
    },
    components: {},
    created() {
      this.getOrderList(this.orderList.page, this.orderList.pageSize)
    },
    watch: {
      $route () {
        this.type = this.$route.query.type
        this.initOrderList()
        this.getOrderList(this.orderList.page, this.orderList.pageSize)
      }
    },
    methods: {
      countOrderGoodsNum(order) {
        let count = 0
        if (!order || !order.order_goods) {
          return count
        }
        for (let i = 0; i < order.order_goods.length; i++) {
          count += parseInt(order.order_goods[i].goods_num)
        }
        return count
      },
      gotoOrderExpress(id) {
        this.$router.push({name: 'order-express', params: {id: id}})
      },
      gotoOrderDetail(id) {
        this.$router.push({name: 'order-detail', params: {id: id}, query: {userLv: this.userLv, self: this.userLv == this.$dictionary.userLv.a ? 'true' : 'false'}})
      },
      switchType(value) {
//        this.type = value
//        this.initOrderList()
//        this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
        this.$router.replace({name: 'order-list', query: {type: value}})
      },
      initOrderList() {
        this.orderList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
      },
      getOrderList(page, pageSize) {
        this.orderList.loading = true
        let params = {member_id: this.memberId, type: this.userLv, pageSize: pageSize, page: page, s_time: this.s_time, e_time: this.e_time}
        this.$request.getFriendOrderList(params).then(data => {
          if (data.length < this.orderList.pageSize) {
            this.orderList.allLoaded = true
          }
          this.orderList.data = [...this.orderList.data, ...data]
        }).finally(() => {
          this.orderList.requested = true
          this.orderList.loading = false
        })
      },
      loadMoreOrder() {
        if (this.orderList.loading || this.orderList.allLoaded) { // 正在加载 或 已取得全部数据
          return
        }
        setTimeout(() => {
          this.getOrderList(++this.orderList.page, this.orderList.pageSize)
        }, 500)
      },
      cancleOrder(id) {
        MessageBox.confirm(`你确定要取消订单吗?`).then(action => {
          this.$request.cancleOrder(id).then(data => {
            this.$toast('取消订单成功')
            this.initOrderList()
            this.getOrderList(this.orderList.page, this.orderList.pageSize)
          }).catch(e => {
            this.$toast('取消订单失败：' + e.msg)
          })
        }).catch(() => {
        })
      },
      searchOrder() {
      },
      addComment(id) {
        this.$router.push({name: 'comment-add', params: {id: id}})
      },
      addRefundGoods(orderId, goodsId) {
        this.$router.push({name: 'refund-goods-add', params: {orderId: orderId, goodsId: goodsId}})
      },
      addRefundMoney(orderId, goodsId) {
        this.$router.push({name: 'refund-money-add', params: {orderId: orderId, goodsId: goodsId}})
      },
      confirmReceived(id) {
        this.$request.confirmReceived(id).then(data => {
          this.$toast('确认收货成功')
          for (let i = 0; i < this.orderList.data.length; i++) {
            if (this.orderList.data[i].order_id == id) {
              this.orderList.data.splice(i, 1)
              break
            }
          }
        }).catch(e => {
          this.$toast('确认收货失败：' + e.msg)
        })
      },
      pay(order) {
        let paySn = order.pay_sn
        payPop({paySn: paySn, orderId: order.order_id})
      }
    }
  }
</script>

<style>
  .but1 {
    display: inline-block;
    height: 1.3rem;
    line-height: 1.25rem;
    text-align: center;
    font-size: 0.6rem;
    border: #dcdddd solid 1px;
    border-radius: 0.125rem;
    padding: 0 0.375rem;
  }
</style>